﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;

using ChiDao.Component.Utils;
using ChiDao.Business.Models;
using ChiDao.Business.Models.FlowProcessNew;
using ChiDao.Site.Models;
using ChiDao.Business.Data.Repositories.FlowProcessNew;


namespace ChiDao.Business.Service.Impl
{
    [Export(typeof(IBaklogTaskNewService))]
    internal class BaklogTaskNewService : IBaklogTaskNewService
    {
        [Import]
        protected IBaklogTaskNewRepository BaklogTaskNewRepository { get; set; }

        public List<BaklogTaskNewView> GetBaklogTaskNew(string processPerson, string processID = "")
        {
            List<BaklogTaskNewView> baklogTaskNewViewList = new List<BaklogTaskNewView>();
            IQueryable<BaklogTaskNew> baklogTaskNews = null;
            if (string.IsNullOrEmpty(processID))
            {
                baklogTaskNews = BaklogTaskNewRepository.Entities.Where(c => c.CurrentProcessPerson == processPerson);
            }
            else
            {
                baklogTaskNews = BaklogTaskNewRepository.Entities.Where(c => c.ProcessID == processID);
            }
            foreach (BaklogTaskNew baklogTaskNew in baklogTaskNews)
            {
                baklogTaskNewViewList.Add(new BaklogTaskNewView()
                {
                    ID = baklogTaskNew.ID,
                    ProcessID = baklogTaskNew.ProcessID,
                    TaskName = baklogTaskNew.TaskName,
                    TaskType = baklogTaskNew.TaskType,
                    RequestPerson = baklogTaskNew.RequestPerson,
                    CurrentProcessPerson = baklogTaskNew.CurrentProcessPerson,
                    CurrentProcessNodeCode = baklogTaskNew.CurrentProcessNodeCode
                });
            }
            return baklogTaskNewViewList;
        }

        public void AddBaklogTaskNew(List<BaklogTaskNewView> addList)
        {
            foreach (BaklogTaskNewView baklogTaskNewView in addList)
            {
                BaklogTaskNew baklogTaskNew = new BaklogTaskNew()
                {
                    ID = baklogTaskNewView.ID,
                    ProcessID = baklogTaskNewView.ProcessID,
                    TaskName = baklogTaskNewView.TaskName,
                    TaskType = baklogTaskNewView.TaskType,
                    RequestPerson = baklogTaskNewView.RequestPerson,
                    CurrentProcessPerson=baklogTaskNewView.CurrentProcessPerson,
                    CurrentProcessNodeCode = baklogTaskNewView.CurrentProcessNodeCode
                };
                BaklogTaskNewRepository.Insert(baklogTaskNew);
            }
        }

        public void DeleteBaklogTaskNew(List<BaklogTaskNewView> deleteList)
        {
            foreach (BaklogTaskNewView baklogTaskNewView in deleteList)
            {
                BaklogTaskNewRepository.Delete(baklogTaskNewView.ID);
            }
        }
    }
}